package com.my.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.entity.Curriculum;
import com.my.entity.GradeClass;
import com.my.service.CurriculumService;
import com.my.service.GradeClassService;
import com.my.util.Rest;
import com.my.util.execl.CurriculumExeclListener;
import com.my.util.execl.entity.CurriculumExecl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("curriculum")
public class CurriculumController {

    @Autowired
    private CurriculumService service;

    @Autowired
    private GradeClassService gcService;

    @GetMapping("list")
    public String list(Model model, @RequestParam(defaultValue = "1") int pageNo, Curriculum curriculum) {
        model.addAttribute("gradeList", gcService.list(new QueryWrapper<GradeClass>().eq("deep", 1)));
        model.addAttribute("page", service.getList(new Page<>(pageNo, 10), curriculum));
        model.addAttribute("name", curriculum.getName());
        model.addAttribute("week", curriculum.getWeek());
        model.addAttribute("gId", curriculum.getGId());
        model.addAttribute("gradeClassId", curriculum.getGradeClassId());
        return "curriculum/list";
    }

    @GetMapping("edit")
    public String edit(Model model, Long id) {
        model.addAttribute("gradeList", gcService.list(new QueryWrapper<GradeClass>().eq("deep", 1)));
        if(id != null) {
            Curriculum curriculum = service.getById(id);
            curriculum.setGId(gcService.getById(curriculum.getGradeClassId()).getParentId());
            model.addAttribute("curriculum", curriculum);
        }
        return "curriculum/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    public Rest edit(Curriculum curriculum) {
        if(curriculum.getId() == null ? service.save(curriculum): service.updateById(curriculum)) {
            return Rest.ok("保存成功！");
        }
        return Rest.err("保存失败！");
    }

    @PostMapping("del")
    @ResponseBody
    public Rest del(Long id) {
        if(service.removeById(id)) {
            return Rest.ok("删除成功！");
        }
        return Rest.err("删除失败！");
    }

    @PostMapping("import")
    @ResponseBody
    @Transactional
    public Rest importData(@RequestParam("file") MultipartFile file) {
        if(file.isEmpty()) {
            return Rest.err("上传文件不能为空!");
        }
        String fileName = file.getOriginalFilename();
        if(fileName.lastIndexOf(".") == -1) {
            return Rest.err("请上传正确的文件!");
        }
        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
        if(!(suffixName.toLowerCase().equals("xls") || suffixName.toLowerCase().equals("xlsx"))) {
            return Rest.err("该文件格式不支持！");
        }
        try {
            EasyExcel.read(file.getInputStream(), CurriculumExecl.class, new CurriculumExeclListener(service, gcService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            return Rest.err("请检查导入文件数据格式！");
        }
        return Rest.ok("导入成功！");
    }

}
